<?php

class Twitter {

	private $username;
	private $rawData;

	public function __construct($username, $count) {
  
		$this->username = $username;
		
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, "https://api.twitter.com/1/statuses/user_timeline.json?include_entities=true&include_rts=true&screen_name={$username}&count={$count}");
		curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 3);
		curl_setopt($ch, CURLOPT_TIMEOUT, 3);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 1);
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
		$output = curl_exec($ch);
		curl_close($ch);
				
		// Get XML
		$this->rawData = json_decode($output, true);
	  		
	}
	
	public function getTopFeed() {
		
		$return = array();
				
		if(!empty($this->rawData)) {
			
			foreach($this->rawData as $k => $v) {
				
				$return[$k] = array('title' => $v['text'], 'linkTime' => '<a style="border: 0px;" href="https://twitter.com/'.$this->username.'/status/'.$v['id_str'].'">'.$v['created_at'].'</a>');
				
			}
					
		} else {
			
			$return[0] = array('title' => 'Twitter is currently experiencing technical difficulties.', 'linkTime' => '');
			
		}
		
		return $return;
				
	}
	
}

$twitter = new Twitter('livuni', 5);
$return = $twitter->getTopFeed();

# Return response
echo(json_encode($return));

?>